home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CFlexiDataFile 1.1 / CFlexiDataFile.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  5.7 KB  |  176 lines  |  [TEXT/KAHL]

  1. /*
  2.  * CFlexiDataFile.h
  3.  *
  4.  * A subclass of CDataFile that can pretend that a resource,
  5.  * a handle, or a section of memory is actually a file on disk.
  6.  *
  7.  * © Copyright 1992 by Jamie R. McCarthy.  All rights reserved.
  8.  * This code can be both distributed and used freely.
  9.  * Internet: k044477@kzoo.edu            AppleLink: j.mccarthy
  10.  * Telephone:  800-421-4157 or US 616-665-7075 (9:00-5:00 Eastern time)
  11.  * I'm releasing this code with the hope that someone will get something
  12.  * out of it.  Feedback of any sort, even just letting me know that you're
  13.  * using it, is greatly appreciated!
  14.  *
  15.  * Read the comment at the beginning of CFlexiDataFile.c.
  16.  *
  17.  */
  18.  
  19.  
  20.  
  21. /********************************/
  22.  
  23. #pragma once
  24.  
  25. /********************************/
  26.  
  27. #include <CDataFile.h>
  28.  
  29. /********************************/
  30.  
  31. enum {
  32.     kModeUnspecified,
  33.     kModeDataFork,
  34.     kModeResource,
  35.     kModeHandle,
  36.     kModeMemory
  37. } ;
  38.  
  39. /********************************/
  40.  
  41.     /*
  42.      * Here's an added bonus for you.  I stuck this in because it makes
  43.      * it easy to integrate an open-a-resource-as-a-file dialog with the
  44.      * TCL.  If you're not using the TCL, you can skip this comment.
  45.      *
  46.      * You can munge data in a SFReply record and pass it to a CFlexiDataFile,
  47.      * to indicate to it that it should open, not a regular file, but a
  48.      * resource or a handle.  For example, if you want to present an "open a
  49.      * resource as a file" dialog instead of the standard file dialog, just
  50.      * override CApplication::ChooseFile to present your dialog.  Then put
  51.      * the ResType and ID chosen into the appropriate places in the SFReply
  52.      * record, set the version field to kResFileFlag, and return.
  53.      * CApplication will cheerfully pass the munged SFReply to
  54.      * CYourApp::OpenDocument, which will cheerfully create a doc and pass
  55.      * it to CYourDoc::OpenFile.  CYourDoc::OpenFile will cheerfully create
  56.      * a CFlexiDataFile and pass the same munged SFReply to it, the
  57.      * CFlexiDataFile will recognize the flag and open the resource as a
  58.      * file, and no one will be any the wiser.
  59.      *
  60.      * To open a resource as a data file, set the version field to
  61.      * kResFileFlag, the fType field to the resource's type, and the
  62.      * vRefNum field to the resource's ID.
  63.      *
  64.      * To open a handle as a data file, set the version field to
  65.      * kHndlFileFlag and the fType field to the handle.
  66.      *
  67.      * To open a section of memory as a data file, set the version
  68.      * field to kMemoryFileFlag, the fType field to the handle, and
  69.      * the vRefNum field to the length.  (Sorry--if you want to
  70.      * access more than 64K, you'll have to really use
  71.      * memorySpecify().  There's no more room in the SFReply record.)
  72.      *
  73.      * I really have to stretch to come up with a real-world situation
  74.      * in which you'd want the user to be able to specify handles or
  75.      * sections of memory to operate on.  But letting the user specify
  76.      * a resource is definitely useful, and it's easy to let the
  77.      * other two tag along.
  78.      * 
  79.      */
  80.     
  81. enum {
  82.     kResFileFlag = -999,            // Set aSFReply->version to this to indicate a resource
  83.     kHndlFileFlag,                    // Set aSFReply->version to this to indicate a Handle
  84.     kMemoryFileFlag                // Set aSFReply->version to this to indicate a section of memory
  85. } ;
  86.  
  87. /********************************/
  88.  
  89.  
  90.  
  91. class CFlexiDataFile : public CDataFile {
  92.     
  93. public:
  94.     
  95.     void            IFlexiDataFile(void);
  96.     void            Dispose(void);
  97.     
  98.     void            Open(SignedByte permission);                        // override
  99.     void            Close(void);                                            // override
  100.     
  101.     Boolean        IsOpen(void);                                            // not an override!
  102.     
  103.     void            Specify(Str63 aName, short aVolNum);            // override
  104.     void            SpecifyFSSpec(const FSSpec *aFileSpec);        // override
  105.     void            SFSpecify(SFReply *macSFReply);                    // override
  106.     void            ResolveFileAlias(void);                                // override
  107.     void            rsrcSpecify(ResType theRsrcType, short theRsrcID);
  108.     void            hndlSpecify(Handle theHndl);
  109.     void            memorySpecify(void *thePointer, long theLength);
  110.     
  111.     void            GetName(Str63 theName);                                // override
  112.     void            GetFSSpec(FSSpec *aFileSpec);                        // override
  113.     
  114.         /*
  115.          * These two methods could be implemented for resources and handles;
  116.          * actually, it'd be kinda cool to be able to call CreateNew()
  117.          * instead of AddResource().  But I don't need them, so I haven't
  118.          * gotten around to making either of these work for resources and
  119.          * handles.
  120.          */
  121.     void            CreateNew(OSType creator, OSType fType);        // override
  122.     void            ThrowOut(void);                                        // override
  123.     
  124.     void            ChangeName(Str63 newName);                            // override
  125.     Boolean        ExistsOnDisk(void);                                    // override
  126.     void            GetMacFileInfo(FInfo *fileInfo);                    // override
  127.  
  128.     void            SetLength(long aLength);                            // override
  129.     long            GetLength(void);                                        // override
  130.     void            SetMark(long howFar, short fromWhere);            // override
  131.     long            GetMark(void);                                            // override
  132.     
  133.     Handle        ReadAll(void);                                            // override
  134.     void            ReadSome(Ptr info, long howMuch);                // override
  135.     void            WriteAll(Handle contents);                            // override
  136.     void            WriteSome(Ptr info, long howMuch);                // override
  137.     
  138.     short            getMode(void);
  139.     
  140.     
  141. protected:
  142.     
  143.     short                    itsPermission;
  144.     
  145.     short                    itsMode;
  146.     
  147.         /* Used when itsMode is either kModeResource or kModeHandle. */
  148.     Handle                itsDataHndl;
  149.         /* Used only when itsMode is kModeMemory. */
  150.     void                    *itsDataPointer;
  151.     long                    itsDataLength;
  152.     
  153.         /* Used when itsMode is kModeResource, kModeHandle, or kModeMemory. */
  154.     long                    itsDataMark;
  155.     
  156.         /* Used only when itsMode is kModeHandle. */
  157.     short                    itsOldHState;
  158.     
  159.         /* Used only when itsMode is kModeResource. */
  160.     ResType                itsRsrcType;
  161.     short                    itsRsrcID;
  162.     
  163.     
  164.         /* Raises an exception if the file has read-only permission. */
  165.     void            testWritePermission(void);
  166.     
  167.         /*
  168.          * Raises an exception if the file is open in memory mode, and the
  169.          * section of memory specified by itsDataPointer/itsDataLength is
  170.          * invalid.
  171.          */
  172.     void            testMemoryValidity(void);
  173.     
  174.     
  175. } ;
  176.